﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using CTIClassLibrary;

namespace CTIPayService
{
    public partial class CallBackCPNR : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Common.DB.OnLog += new NBearLite.LogHandler(DB_OnLog);
            DateTime date = DateTime.Now;
 
            string OrdId = Request.Form["OrdId"];
            string SysDate = Request.Form["SysDate"];
            string SysSeqId = Request.Form["SysSeqId"];
            string TransStat = Request.Form["TransStat"];

            string ChkValue = Request.Form["ChkValue"];
            string msgData = "";
            string Version = "20";//版本号
            string MerId = Request.Form["MerId"];
            string TransAmt = Request.Form["TransAmt"];
            string MerDate = Request.Form["MerDate"];
            string TransType = Request.Form["TransType"];
            string GateId = Request.Form["GateId"];
            string MerPriv = Request.Form["MerPriv"];
            string RetUrl = "http://221.232.148.26:3128/CTIPayService/CallBackCPNR.aspx";

            //msgData = Version + MerId + OrdId + TransAmt + MerDate + TransType + GateId + MerPriv + RetUrl;
            msgData = MerId + OrdId + TransAmt + MerDate + TransType + GateId + MerPriv + SysDate + SysSeqId;
            byte[] msgBytes = Encoding.Default.GetBytes(msgData);
            CHINAPNRLib.NetpayClientClass cpnr = new CHINAPNRLib.NetpayClientClass();
            string keyFile = Server.MapPath("./");
            keyFile = System.IO.Path.Combine(keyFile, "PgPubk.key");

            try
            {
                string checkSum = "";

                try
                {
                    checkSum = cpnr.VeriSignMsg(keyFile, msgData, msgBytes.Length, ChkValue);
                    //Response.Write(">" + checkSum + "<");
                }
                catch
                {
                    throw new Exception("回调参数错误! MsgData=" + msgData + " ChkValue=" + ChkValue);
                }

                if (checkSum == "0")//签名有效
                {
                    Common.DB.Insert(Tables.t_CPNR_CallBackHistory)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.Datetime, date)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.QueryString, Request.Url.AbsoluteUri)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.RemoteIP, Request.UserHostAddress)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.Remarks, "正常")
                        .Execute();
                    Common.DB.Update(Tables.t_CPNR_Transfer)
                        .AddColumn(Tables.t_CPNR_Transfer.SysDate, SysDate)
                        .AddColumn(Tables.t_CPNR_Transfer.SysSeqId, SysSeqId)
                        .AddColumn(Tables.t_CPNR_Transfer.transStat, TransStat)
                        .AddColumn(Tables.t_CPNR_Transfer.CallBackDatetime, date)
                        .Where(Tables.t_CPNR_Transfer.OrdId == OrdId)
                        .Execute();

                    Response.Write("RECV_ORD_ID_" + OrdId);
                    Response.End();
                }
                else
                {
                    Common.DB.Insert(Tables.t_CPNR_CallBackHistory)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.Datetime, date)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.QueryString, Request.Url.AbsoluteUri)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.RemoteIP, Request.UserHostAddress)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.Remarks, "非法 checkSum=" + checkSum + " MsgData=" + msgData + " ChkValue=" + ChkValue)
                        .Execute();
                }
            }
            catch (Exception ee)
            {
                this.txtInfo.Text += ee.Message;
                Common.DB.Insert(Tables.t_CPNR_CallBackHistory)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.Datetime, date)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.QueryString, Request.Url.AbsoluteUri)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.RemoteIP, Request.UserHostAddress)
                        .AddColumn(Tables.t_CPNR_CallBackHistory.Remarks, "错误 ErrorMsg=" + ee.Message)
                        .Execute();
            }
        }

        void DB_OnLog(string logMsg)
        {
            this.txtInfo.Text += logMsg;
        }
    }
}
